import { Prisma } from '@prisma/client';
import { Transform, Type } from 'class-transformer';
import {
  IsDefined,
  IsNotEmptyObject,
  Max,
  Min,
  ValidateNested,
} from 'class-validator';
import { BaseDto, BaseFindDto, BaseQueryDto, WhiteListItem } from './base.dto';

export class PlanCreateDto extends BaseDto<Prisma.PlanCreateInput> {}

export class PlanUpdateDto extends BaseDto<Prisma.PlanUpdateInput> {
  @IsDefined({ message: '条件必填' })
  @IsNotEmptyObject({ nullable: false }, { message: '条件参数必填' })
  where: Prisma.PlanWhereUniqueInput;
}

export class PlanRemoveDto {
  @IsDefined({ message: 'ID必填' })
  @Transform(({ value }) => Number(value))
  id: number;
}

export class PlanFindDto extends BaseFindDto<
  Prisma.PlanWhereUniqueInput,
  Prisma.PlanSelect
> {}

export class PlanQueryDto extends BaseQueryDto<Prisma.PlanWhereUniqueInput> {}

export class PlanListDto extends BaseQueryDto<Prisma.PlanWhereUniqueInput> {}

export class PlanLoadDto {
  @IsDefined({ message: 'ID必填' })
  @Transform(({ value }) => Number(value))
  id: number;

  @ValidateNested({ each: true })
  @Type(() => WhiteListItem)
  rows?: WhiteListItem[];
}

export class PlanOrderDto {
  @IsDefined({ message: 'ID必填' })
  @Transform(({ value }) => Number(value))
  id: number;

  @IsDefined({ message: '数量必填' })
  @Transform(({ value }) => Number(value))
  @Min(1, { message: '数量不能小于1' })
  @Max(100, { message: '数量不能大于100' })
  num?: number;
}

export class PlanOrderItem {
  @IsDefined({ message: '手机号必填' })
  mobile: string;

  @IsDefined({ message: '数量必填' })
  @Transform(({ value }) => Number(value))
  @Min(1, { message: '数量不能小于1' })
  @Max(100, { message: '数量不能大于100' })
  num: number;
}

export class PlanRpaOrderDto {
  @IsDefined({ message: 'ID必填' })
  @Transform(({ value }) => Number(value))
  id: number;

  @ValidateNested({ each: true })
  @Type(() => PlanOrderItem)
  rows?: PlanOrderItem[];
}

export class PlanRpaImportDto {
  @IsDefined({ message: 'ID必填' })
  @Transform(({ value }) => Number(value))
  id: number;

  @ValidateNested({ each: true })
  @Type(() => PlanOrderItem)
  rows?: PlanOrderItem[];
}
